package com.example.web.server.impl;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import com.example.web.server.SendEmail;

import lombok.extern.slf4j.Slf4j;
@Slf4j
@Component
public class SendImg implements SendEmail{

	private String mailTemplate = null;
	@Value("${spring.mail.template.img}")
	private String templateUrl;
	
	@Override
	public String type() {
		return "img";
	}

	@Override
	public String spellTempalte(String to) {
		String fileUri="/email"+"/mail-img.template";
		try {
			if (mailTemplate == null) {
				StringBuilder buffer = new StringBuilder();
				try (InputStream stream = new ClassPathResource(fileUri).getInputStream()) {
					try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
						String line;
						while ((line = reader.readLine()) != null) {
							buffer.append(line);
						}
					}
				}
				mailTemplate = buffer.toString();
			}
			 String body = mailTemplate.replaceAll("template_img", templateUrl);
			
			return body;
		} catch (Exception e) {
			log.error("send message error", e);
			return null;
		}
	}

	

}
